package com.neo.agent.connection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lock on 17-8-3.
 */
public class AvocationView {

    private List<String> a = new ArrayList<>();

    private List<String> b = new ArrayList<>();

    private List<String> c = new ArrayList<>();


    public List<String> getA() {
        return a;
    }

    public void setA(List<String> a) {
        this.a = a;
    }

    public List<String> getB() {
        return b;
    }

    public void setB(List<String> b) {
        this.b = b;
    }

    public List<String> getC() {
        return c;
    }

    public void setC(List<String> c) {
        this.c = c;
    }

    public static String getterMethod(String s){
        char c = s.charAt(0);

        return "get"+((""+c).toUpperCase())+s.substring(1,s.length());
    }

    private interface EmptyListener{

        void empty(Object o,boolean isEmpty);
    }

    public static void matchList(Object o,EmptyListener listener){
        Class<?> origin = o.getClass();
        Field[] fields = origin.getDeclaredFields();
        //getC
        int total = 0;
        int count =0;
        for(Field f : fields){
            if(f.getType().isAssignableFrom(List.class)){
                total++;
                Method method = null;
                try {
                    method = origin.getDeclaredMethod(getterMethod(f.getName()));
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                    //ignore
                    continue;
                }
                List<?> result = null;
                try {
                    result = (List<?>) method.invoke(o);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                    //ignore
                    continue;
                }

                if(result.isEmpty()){
                    count++;
                }
            }
        }
        listener.empty(o,(total==count));
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        AvocationView avocationView = new AvocationView();
        //avocationView.getC().add("t");
        matchList(avocationView, (o, isEmpty) -> {
            if(isEmpty){
                System.out.println(" empty !");
            }else{
                System.out.println(" empty !xxx");
            }

        });
    }

}
